// let baseUrl = '/dpc' //本地路径
// let baseUrl = 'http://bkda.kaigekeji.com' //app路径
let baseUrl = 'http://zoufang.tanhuajy.cn'
const post = (url, data) => {
	// uni.showLoading({
	// 	mask: true,
	// })
	let token = '';
	try {
		const value = uni.getStorageSync('token');
		if (value) {
			token = value
			// console.log(token)
		}
	} catch (e) {
		// error
	}


	let httpDefaultOpts = {
		url: baseUrl + url,
		data: data,
		method: "POST",
		header: {
			'token': token,
			// 'AppRQ':1,
			"Content-Type": "application/x-www-form-urlencoded",
		},
		// dataType:'json'
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				// console.log(res);
				// uni.hideLoading();
				if (res[1].data.code == 1) {
					resolve(res[1].data)
					return;
				}
				if (res[1].data.code == 3) {
					uni.showToast({
						icon: 'none',
						title: res[1].data.msg
					})
					// 	setTimeout(function() {
					// 			uni.reLaunch({
					// 				url: '/pages/login/login'
					// 			})

					// 	}, 1500)
					// 	return;
				}
				if (res[1].data.code == 3) {
					// return;
					uni.showToast({
						icon: 'none',
						title: res[1].data.msg
					})
					setTimeout(function() {
						uni.reLaunch({
							url: '/pages/login/login'
						})

					}, 1500)
					return;
				}
				// if(res[1].data.ErrorCode === -2||res[1].data.ErrorCode === -10){
				//   uni.reLaunch({
				//     url:'/pages/login/login'
				//   })
				// }
				resolve(res[1].data)
			}
		).catch(
			(response) => {
				uni.hideLoading();
				// console.log(response);
				if (response[1].code === 0 || response[1].code === 2) {
					uni.reLaunch({
						url: '/pages/login/login'
					})
				}
				reject(response)
			}
		)
	})
	return promise
};
const get = (url, data) => {
	let token = '';
	try {
		const value = uni.getStorageSync('token');
		if (value) {
			token = value
		}
	} catch (e) {
		// error
	}
	let httpDefaultOpts = {
		url: baseUrl + url,
		method: "GET",
		data: data,
		header: {
			'Token': token,
			// 'AppRQ':1,
			// 'X-Requested-With': 'XMLHttpRequest',
			"Content-Type": "application/json",
		},
		dataType: 'json'
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				uni.hideLoading();
				// console.log(res)
				if (res[1].code != 1) {
					uni.showToast({
						icon: 'none',
						title: res[1].data.msg
					})

					setTimeout(function() {
						if (res[1].code == 0 || res[1].code == 2) {
							uni.reLaunch({
								url: '/pages/login/login'
							})
						}
					}, 1500)
					return;
				}
				resolve(res[1].data)
			}
		).catch(
			(response) => {
				uni.hideLoading();
				reject(response)
			}
		)
	})
	return promise
};


export default {
	post,
	get
}
